package com.envper.controller.cls;

import com.envper.common.domain.dto.ClassDto;
import com.envper.common.service.cls.ClassService;
import com.envper.mysql.domain.Result;
import com.envper.mysql.domain.Status;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/class")
public class ClassController {
    private final ClassService classService;

    @GetMapping("/list")
    public Result<List<ClassDto>> getClassList(@RequestParam(name = "userId", required = false) Long userId) {
        List<ClassDto> classList;
        if (ObjectUtils.isEmpty(userId)) {
            classList = classService.getClassList();
        } else {
            classList = classService.getClassListByUserId(userId);
        }
        return Result.ok(Status.SUCCESS, classList);
    }

    @PostMapping("/create")
    public Result<String> createClass(@RequestBody ClassDto classDto) {
        classService.createClass(classDto);
        return Result.ok(Status.CREATESUCCESS);
    }

    @GetMapping("/info")
    public Result<ClassDto> getClassInfo(@RequestParam(name = "id") Long id) {
        ClassDto classDto = classService.getClassInfoById(id);
        return Result.ok(Status.SUCCESS, classDto);
    }

    @PostMapping("/bind")
    public Result<String> bindDevice(@RequestParam("classId") Long classId, @RequestParam("deviceId") Long deviceId) {
        classService.bindDevice(classId, deviceId);
        return Result.ok(Status.BindSuccess);
    }


}
